package com.lzb.permanentcalendar.fragment;

import androidx.fragment.app.Fragment;

import android.view.View;

import com.blankj.utilcode.util.TimeUtils;
import com.lzb.permanentcalendar.BaseNoModelFragment;
import com.lzb.permanentcalendar.R;
import com.lzb.permanentcalendar.databinding.FragmentDateCalculationBinding;
import com.lzb.permanentcalendar.dialog.SelDateDialog;
import com.lzb.permanentcalendar.utils.DateCalculator;
import com.nlf.calendar.Lunar;
import com.nlf.calendar.Solar;

import java.text.SimpleDateFormat;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link DateCalculationFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class DateCalculationFragment extends BaseNoModelFragment<FragmentDateCalculationBinding> implements View.OnClickListener {

    private SelDateDialog selDateDialog;
    private long selDate;

    @Override
    protected int onCreate() {
        return R.layout.fragment_date_calculation;
    }

    @Override
    protected void initView() {
        mDataBinding.llSelDate.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        selDate = TimeUtils.getNowMills();
        String tip = getString(R.string.until_num) + TimeUtils.millis2String(selDate, new SimpleDateFormat("yyyy年MM月dd日")) + getString(R.string.still);
        mDataBinding.tvSelDate.setText(TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd")));
        mDataBinding.tvDateTip.setText(tip);
        mDataBinding.tvInterval.setText(0 + "天");
    }

    @Override
    public void onClick(View view) {
        int id=view.getId();
        if (id==R.id.llSelDate){//选择日期
            showSelDateDialog();
        }
    }

    private void showSelDateDialog() {
        if (selDateDialog == null) selDateDialog = new SelDateDialog(mContext);
        selDateDialog.setListener(new SelDateDialog.Ilistener() {
            @Override
            public void clickConfirm(int type, int year, int mo, int days) {
                if (type == 0) {
                    String date = year + "-" + (mo < 10 ? "0" + mo : mo) + "-" + (days < 10 ? "0" + days : days);
                    selDate = TimeUtils.string2Millis(date, new SimpleDateFormat("yyyy-MM-dd"));
                    mDataBinding.tvSelDate.setText(date);
                } else if (type == 1) {
                    Lunar lunar = new Lunar(year, mo, days);
                    // 转换为阳历
                    Solar solar = lunar.getSolar();
                    String date = solar.getYear() + "-" + (solar.getMonth() < 10 ? "0" + solar.getMonth() : solar.getMonth()) + "-" + (solar.getDay() < 10 ? "0" + solar.getDay() : solar.getDay());
                    selDate = TimeUtils.string2Millis(date, new SimpleDateFormat("yyyy-MM-dd"));
                    mDataBinding.tvSelDate.setText(date);
                }
                int num = DateCalculator.getDaysBetweenExact(selDate, TimeUtils.getNowMills());
                if (num < 0) {
                    String tip = getString(R.string.until_num) + TimeUtils.millis2String(selDate, new SimpleDateFormat("yyyy年MM月dd日")) + getString(R.string.already);
                    mDataBinding.tvDateTip.setText(tip);
                    mDataBinding.tvInterval.setText(Math.abs(num) + "天");
                } else {
                    String tip = getString(R.string.until_num) + TimeUtils.millis2String(selDate, new SimpleDateFormat("yyyy年MM月dd日")) + getString(R.string.still);
                    mDataBinding.tvDateTip.setText(tip);
                    mDataBinding.tvInterval.setText(Math.abs(num) + "天");
                }
            }
        });
        selDateDialog.show();
    }
}